﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class ApplicationInfo
{
    private string _location="";
    private string _className="";
    public fApp.ebApp inst = null;
    const string appFileName = "fap";

    private ApplicationInfo(string path, string cls)
    {
        this.Location = path;
        this.ClassName = cls;
    }

    /// <summary>
    /// アセンブリファイルのパス
    /// </summary>
    public string Location
    {
        get { return _location; }
        set {_location = value;}
    }

    /// <summary>
    /// クラスの名前
    /// </summary>
    public string ClassName
    {
        get { return _className; }
        set { _className = value; }
    }


    public static ApplicationInfo[] FindPlugins()
    {
        
        System.Collections.ArrayList plugins =
            new System.Collections.ArrayList();
        string ipluginName = typeof(fApp.ebApp).FullName;

        string folder = System.IO.Path.GetDirectoryName(
            System.Reflection.Assembly
            .GetExecutingAssembly().Location);
        folder += "\\app";
        if (!System.IO.Directory.Exists(folder))
            throw new ApplicationException(
                "app\"" + folder +
                "\" is not found.");

        string[] dlls =
            System.IO.Directory.GetFiles(folder, "*."+appFileName);

        foreach (string dll in dlls)
        {
            try
            {
                System.Reflection.Assembly asm =
                    System.Reflection.Assembly.LoadFrom(dll);
                foreach (Type t in asm.GetTypes())
                {
                    if (t.IsClass && t.IsPublic && !t.IsAbstract &&
                        t.GetInterface(ipluginName) != null)
                    {
                        plugins.Add(
                            new ApplicationInfo(dll, t.FullName));
                    }
                }
            }
            catch
            {
            }
        }

        //コレクションを配列にして返す
        return (ApplicationInfo[])plugins.ToArray(typeof(ApplicationInfo));
    }

    /// <summary>
    /// プラグインクラスのインスタンスを作成する
    /// </summary>
    /// <returns>プラグインクラスのインスタンス</returns>
    public void CreateInstance()
    {
        try
        {
            //アセンブリを読み込む
            System.Reflection.Assembly asm =
                System.Reflection.Assembly.LoadFrom(this.Location);
            //クラス名からインスタンスを作成する
            inst= (fApp.ebApp)
                asm.CreateInstance(this.ClassName);
        }
        catch
        {
        }
    }
    public System.Drawing.Bitmap getIcon()
    {
        try
        {
            //アセンブリを読み込む
            System.Reflection.Assembly asm =
                System.Reflection.Assembly.LoadFrom(this.Location);
            //クラス名からインスタンスを作成する
            return new System.Drawing.Bitmap(asm.GetManifestResourceStream(asm.GetName().Name+".icon.png"));
        }
        catch
        {

            System.Reflection.Assembly asm; 
            asm = System.Reflection.Assembly.GetExecutingAssembly();
            return new System.Drawing.Bitmap(asm.GetManifestResourceStream("fApp.icon.png"));
        }
    }
}